package com.budweiser.user.service.impl;

import com.budweiser.commons.utils.PageUtil;
import com.budweiser.model.page.Page;
import com.budweiser.model.user.Permission;
import com.budweiser.user.dao.PermissionDao;
import com.budweiser.user.dao.RolePermissionDao;
import com.budweiser.user.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
	@Autowired
	private RolePermissionDao rolePermissionDao;
	@Autowired
	private PermissionDao permissionDao;

	@Override
	public Set<Permission> findByRoleIds(Set<Long> roleIds) {

		log.info("根据角色集合id查询权限结合对象", roleIds);
		return rolePermissionDao.findPermissionsByRoleIds(roleIds);
	}

	@Override
	public void save(Permission permission) {
		permission.setCreateTime(new Date());
		permission.setUpdateTime(new Date());
		permission.setDeleteStatus(false);
		log.info("新增权限信息{}", permission);
		permissionDao.insert(permission);
	}

	@Override
	public void update(Permission permission) {
		permission.setUpdateTime(new Date());
		log.info("修改权限信息{}", permission);
		permissionDao.update(permission);
	}

	@Override
	public void delete(List<Long> ids) {
		log.info("删除权限信息{}", ids);
		permissionDao.updateDelStatus(ids);

	}

	@Override
	public Page<Permission> findPermissions(Map<String, Object> params) {
		int count = permissionDao.count(params);
		List<Permission> permissions = Collections.emptyList();
		if (count > 0) {
			PageUtil.pageParamConver(params, true);
			permissions = permissionDao.findData(params);
		}
		return new Page<Permission>(count, permissions);
	}



	

}
